import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import SysOperationLog from '@main/entities/service/admin/sys/sys-operation-log.entity';
import { Repository, Between } from 'typeorm';

@Injectable()
export class SysOperationLogService {
  constructor(
    @InjectRepository(SysOperationLog)
    private operationLogRepository: Repository<SysOperationLog>
  ) {}

  /**
   * 罗列所有配置
   */
  async page(data) {
    const { pageSize, pageNum, entity = {} } = data;

    const { createTime } = entity;

    const where = {};

    if (createTime && createTime.length)
      where['createTime'] = Between(createTime[0], createTime[1]);

    return await this.operationLogRepository.findAndCount({
      where,
      order: {
        updateTime: 'DESC',
      },
      take: pageSize,
      skip: (pageNum - 1) * pageSize,
    });
  }

  /**
   * 新增
   */
  async add(data, user) {
    await this.operationLogRepository.insert({
      ...data,
      databaseOperationLogList: JSON.stringify(data.databaseOperationLogList),
      createName: user?.nickName || null,
      createUserName: user?.userName || null,
      updateName: user?.nickName || null,
      updateUserName: user?.userName || null,
    });
  }

  /**
   * 删除
   */
  async delete(idList) {
    await this.operationLogRepository.delete(idList);
  }
}
